﻿using Coldairarrow.Entity.Base_Company;
using Coldairarrow.Entity.Foot;
using Coldairarrow.IBusiness.Company;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_Company
{
    public class Base_PartnerBusiness : BaseBusiness<BTB_Partner>, IBTB_PartnerBusiness, ITransientDependency
    {
        public Base_PartnerBusiness(IDbAccessor db)
              : base(db)
        {
        }
        public async Task<PageResult<BTB_Partner>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<BTB_Partner>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<BTB_Partner, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }
            return await q.Where(where).GetPageResultAsync(input);
        }
        public async Task<BTB_Partner> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(BTB_Partner data)
        {
            BTB_Partner data1 = new BTB_Partner()
            {
                id = GuidHelper.GenerateKey(),
                Name = data.Name,
                Url = data.Url,
                CreatorId = data.CreatorId,
                CreateTime = data.CreateTime,
                Deleted = false,
                Editor = data.Editor,
                EditTime = data.EditTime,
                Sort = data.Sort,
            };
            await InsertAsync(data1);
        }

        public async Task UpdateDataAsync(BTB_Partner data)
        {
            BTB_Partner data1 = new BTB_Partner()
            {
                id = data.id,
                Name = data.Name,
                Url = data.Url,
                CreatorId = data.CreatorId,
                CreateTime = data.CreateTime,
                Deleted = data.Deleted,
                Editor = data.Editor,
                EditTime = data.EditTime,
                Sort = data.Sort,
            };
            await UpdateAsync(data1);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }
        public async Task<List<BTB_Partner>> GetDataAsync()
        {
           return await GetIQueryable().OrderBy("sort","desc").ToListAsync();
        }
    }
}
